//
//  ZWPropertyHeadView.m
//  NewProject
//
//  Created by tongtong on 2018/5/18.
//  Copyright © 2018年 chenIos. All rights reserved.
//

#import "ZWPropertyHeadView.h"
@interface ZWPropertyHeadView()
@property (nonatomic,strong) UIImageView *backImage; //背景视图
@property (nonatomic,strong) UIImageView *headImage; //头像
@property (nonatomic,strong) UILabel *nickName;     //昵称
@property (nonatomic,strong) UILabel *keyLabel;    //密钥
@property (nonatomic,strong) UIButton *copyBtn;   //登录
@property (nonatomic,strong) UILabel *moneyLabel;    //资金量
@property (nonatomic,strong) UILabel *descriLabel;    //资金描述
@property (nonatomic,strong) UIButton *addBtn;   //添加密钥账户

@end

@implementation ZWPropertyHeadView

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubviews];
    }
    return self;
}

#pragma mark - add subviews
- (void)addSubviews {
    [self addSubview:self.backImage];
    [self.backImage addSubview:self.headImage];
    [self.backImage addSubview:self.nickName];
    [self.backImage addSubview:self.keyLabel];
//    [self.backImage addSubview:self.addBtn];
    [self.backImage addSubview:self.copyBtn];
    [self.backImage addSubview:self.moneyLabel];
    [self.backImage addSubview:self.descriLabel];

    [self initView];
}


#pragma mark - layout subviews
- (void) initView{
//    [super layoutSubviews];
    //  背景
    ZWWeakSelf;
    [_backImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    //  头像x
    [_headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.backImage.mas_top).offset(Scare_Width(50));
        
        make.left.equalTo(weakSelf.backImage.mas_left).offset((SCREEN_W - Scare_Width(46))/2);
        make.size.mas_equalTo(CGSizeMake(Scare_Width(46), Scare_Width(46)));
    }];
    //  昵称
    [_nickName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.headImage.mas_bottom).offset(Scare_Width(5));
        make.left.right.mas_equalTo(0);

//        make.size.mas_equalTo(CGSizeMake(SCREEN_W, Scare_Width(25)));
    }];
    //  密钥
    [_keyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.nickName.mas_bottom).offset(Scare_Width(5));
        make.left.equalTo(weakSelf.backImage.mas_left).offset(SCREEN_W/2 - Scare_Width(60));
        make.width.mas_equalTo(Scare_Width(120));
//        make.size.mas_equalTo(CGSizeMake(Scare_Width(120), Scare_Width(20)));
    }];
    //  复制
    [_copyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.keyLabel.mas_centerY);
        make.left.equalTo(weakSelf.keyLabel.mas_right).offset(Scare_Width(10));
        make.size.mas_equalTo(CGSizeMake(Scare_Width(40), Scare_Width(20)));
    }];
    //  资金量
    [_moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.keyLabel.mas_bottom).offset(Scare_Width(35));
        make.left.equalTo(weakSelf.backImage.mas_left).offset(Scare_Width(20));
        make.height.mas_equalTo(Scare_Width(27));
//        make.size.mas_equalTo(CGSizeMake(Scare_Width(160), Scare_Width(25)));
    }];
    //  资金描述
    [_descriLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.moneyLabel.mas_bottom).offset(Scare_Width(15));
        make.left.equalTo(weakSelf.moneyLabel.mas_left);
    }];
    //  添加账号
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.moneyLabel.mas_bottom);
        make.right.equalTo(weakSelf.backImage.mas_right).offset(-Scare_Width(20));
        make.size.mas_equalTo(CGSizeMake(Scare_Width(40), Scare_Width(40)));
    }];
    
}
-(void)configWithName:(NSString *)nameStr withAddress:(NSString *)addressStr currencyUnit:(NSString *)currencyUnit money:(NSString *)money{
    
    double moneySign = money.doubleValue;
    if ([currencyUnit isEqualToString:@"CNY"]) {
         self.descriLabel.text = [NSString stringWithFormat:@"%@（￥）",Z_Location(@"totalAssets")];
        moneySign = moneySign * 0.55;
    }else if([currencyUnit isEqualToString:@"USD"]){
        moneySign = moneySign * 0.55/6.386;
         self.descriLabel.text = [NSString stringWithFormat:@"%@（$）",Z_Location(@"totalAssets")];
    }
    self.moneyLabel.text =[NSString stringWithFormat:@"%.2lf",moneySign];
    self.nickName.text = nameStr;
    self.keyLabel.text = addressStr;
}


#pragma mark --复制内容---
-(void)copyContentMethod{
    [self makeToast:@"复制成功！" duration:2.0f position:CSToastPositionCenter];
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = self.keyLabel.text;
//    [self.delegate toFilterController];

}
#pragma amrk ---添加新账号--
-(void)addNewKeyMethod{
    if(self.delegate && [self.delegate respondsToSelector:@selector(addNewPropertyMethod)]){
        [self.delegate addNewPropertyMethod];
    }
}
#pragma mark - lazy load
- (UIImageView *)backImage {
    if (!_backImage) {
        _backImage = [[UIImageView alloc] init];
        _backImage.image = [UIImage imageNamed:@"background"];
        _backImage.contentMode = UIViewContentModeScaleAspectFill;
        _backImage.userInteractionEnabled = YES;
    }
    return _backImage;
}
- (UIImageView *)headImage {
    if (!_headImage) {
        _headImage = [[UIImageView alloc] init];
        _headImage.contentMode = UIViewContentModeScaleAspectFill;
        _headImage.clipsToBounds = YES;
        _headImage.layer.cornerRadius = Scare_Width(23);
        _headImage.userInteractionEnabled = YES;
        _headImage.image = [UIImage imageNamed:@"receiveImag"];
    }
    return _headImage;
}
- (UILabel *)nickName {
    if (!_nickName) {
        _nickName = [[UILabel alloc] init];
       
        _nickName.textColor = COLOR_WHITE;
        _nickName.font = H_Medium2;
//        _nickName.text = @"樱桃小丸子";
        _nickName.textAlignment = NSTextAlignmentCenter;
    }
    return _nickName;
}
- (UILabel *)keyLabel {
    if (!_keyLabel) {
        _keyLabel = [[UILabel alloc] init];
        _keyLabel.textColor = COLOR_WHITE;
         _keyLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
        _keyLabel.font = H_Medium2;
//        _keyLabel.text= @"odosfajsdkafldsafjkdlsafasdjhfklsadkfkasldf";
    }
    return _keyLabel;
}
- (UILabel *)moneyLabel {
    if (!_moneyLabel) {
        _moneyLabel = [[UILabel alloc] init];
        _moneyLabel.textColor = COLOR_WHITE;
        _moneyLabel.text = @"≈ 0";
        _moneyLabel.font = H_Medium0;
    }
    return _moneyLabel;
}
- (UILabel *)descriLabel {
    if (!_descriLabel) {
        _descriLabel = [[UILabel alloc] init];
        _descriLabel.textColor = COLOR_WHITE;
        _descriLabel.text = [NSString stringWithFormat:@"%@（￥）",Z_Location(@"totalAssets")];
        _descriLabel.font = H_Medium2;
    }
    return _descriLabel;
}

- (UIButton *)copyBtn {
    if (!_copyBtn) {
        _copyBtn = [[UIButton alloc] init];
        [_copyBtn setTitle:Z_Location(@"copy") forState:UIControlStateNormal];
        _copyBtn.layer.cornerRadius = Scare_Width(10);
        _copyBtn.layer.borderWidth = 0.5;
        _copyBtn.titleLabel.font = H_Medium5;
        _copyBtn.layer.borderColor = COLOR_WHITE.CGColor;
        [_copyBtn addTarget:self action:@selector(copyContentMethod) forControlEvents:UIControlEventTouchUpInside];
    }
    return _copyBtn;
}
- (UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [[UIButton alloc] init];
        _addBtn.titleLabel.font = H_Medium6;
        _addBtn.hidden = YES;
        [_addBtn setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(addNewKeyMethod) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}


@end
